import Vue from 'vue'
import Vuex from 'vuex'
import api from '../api/index.js'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		platform: 'defult',
		publicCounsel: {},// 公参
		tickets: [],
		identity: {}, // 身份信息
		user: {}, // 用户信息
		hasLogin: false,
		wallet: {
			diamondNum: 0,// 钻石数量
			goldNum: 0 //金币数量
		}
	},
	actions: {
		/* 登录 */
		login(store, provider) {
			store.commit('LOGIN', provider);
		},
		/* 获取ticket */
		GetTicket(store, provider) {
			store.commit('GETTICKET', provider);
		},
		/* 更新钱包信息 */
		updateWallet(store, provider) {
			return new Promise((resolve, reject) => {
				api.getWallet({
					uid: store.state.identity.uid
				}).then(res => {
					store.commit('UPDATEWALLET', res.data)
					resolve(res)
				}).catch(error => {
					reject(error)
				})
			})
		},
		// 获取用户信息
		GetInfo(store, provider) {
			return new Promise((resolve, reject) => {
				api.getUser({
					uid: provider.uid
				}).then(res => {
					store.commit('SETUSERINFO', res.data)
					resolve(res)
				}).catch(error => {
					reject(error)
				})
			})
		},
		LogOut({
			commit
		}) {
			commit('LOGOUT')
		},
		/* 获取用户信息 */
		setUserInfo(store, provider) {
			store.commit('SETUSERINFO', provider);
		}
	},
	mutations: {
		/* 存储公参 */
		SETPUBLICCOUNSEL(state, provider) {
			state.publicCounsel = provider;
		},
		/* 登录 */
		LOGIN(state, provider) {
			state.hasLogin = true;
			state.identity = provider; // 身份信息
			uni.setStorage({
				key: 'identity',
				data: provider
			})
		},
		/* 存储用户信息 */
		SETUSERINFO(state, provider) {
			console.log('存储用户信息======================')
			console.log(provider)
			state.user = provider;
		},
		/* 存储ticket */
		GETTICKET(state, provider) {
			console.log('存储ticket信息======================')
			console.log(provider)
			state.tickets = provider;
			uni.setStorage({
				key: 'tickets',
				data: provider
			})
		},
	    /* 更新钱包信息 */
		UPDATEWALLET(state, provider) {
			state.wallet = provider;
		},
		LOGOUT(state) {
			state.hasLogin = false
			state.user = {};
			state.identity = {};
			state.tickets = [];
			uni.removeStorage({
				key: 'tickets',
				success: function(res) {
					console.log("退出成功")
				},
				fail() {

				}
			});
			uni.removeStorage({
				key: 'identity',
				success: function(res) {
					console.log("退出成功")
				},
				fail() {

				}
			});
		}
	}
})

export default store
